package com.scalosoft.action.form2;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;


import com.opensymphony.xwork2.ActionSupport;	
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings({ "serial", "rawtypes" })

@Namespace(value="/form2")
@Result(location = "/form2/Form.jsp")

public class Form extends ActionSupport implements ModelDriven {

	private User user = new User();
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public Object getModel() {
		return user;
	}

	@Action(value = "Check", results = {
			@Result(name = "success", location = "/form2/FormSuccess.jsp"),
			@Result(name = "error", location = "/form2/Error.jsp"),
			@Result(name = "input", location = "/form2/Form.jsp") 		
	})
	
	public String execute() {
		return SUCCESS;
	}

	@Action(value="Form")
	@SkipValidation
	public String init() {
		return SUCCESS;
	}

	public void validate() {
		if (user.getNome().length() == 0)
			addFieldError("user.nome", "campo richiesto");
		if (user.getCognome().length() == 0)
			addFieldError("user.cognome", "campo richiesto");
		if (user.getDataNascita()==null)
			addFieldError("user.dataNascita", "campo richiesto");
	}

}
